import type { UploadProps } from "antd";
import { MENU_TYPE } from "./constants.ts";
export interface ApiUrl {
  login?: string;
  logout?: string;
  register?: string;
  aliossToken?: string;
  minioToken?: string;
  sysConfig?: string;
  getUserInfo?: string;
  getUserList?: string;
  getUserDetail?: string;
  updateUserInfo?: string;
  updateUserStatus?: string;
  updateUserPassword?: string;
}

export type ENV = "dev" | "test" | "prod";

export type Page = {
  key: string;
  content: React.ReactNode;
  path?: string;
};

export type Pages = Page[];
export type CommonNodes = {
  name: string;
  node: React.FC<any>;
}[];

export type PagesMap = Map<string, Page>;
export type CommonNodesMap = Map<string, React.FC<any>>;

export interface AppConfig {
  appId: string;
  appName: string;
  apiBaseUrl: string;
  ossBaseUrl?: string;
  imgError?: string;
  storageType?: "alioss" | "minio";
}

export type Theme = "light" | "dark";

export interface DetailFieldConfig {
  title: string;
  dataIndex: string;
  span?: number;
  render?: (text: any, record: any) => React.ReactNode;
}

export type FormItemType =
  | "input"
  | "password"
  | "number"
  | "textarea"
  | "select"
  | "switch"
  | "date"
  | "dateRange"
  | "time"
  | "timeRange"
  | "upload"
  | "hidden"
  | "richtext"
  | "marktext"
  | "modalPicker"
  | "custom"
  | "cascader"
  | "jsonEditor";

export type FormItemPlaceholder<T extends FormItemType> = T extends "dateRange" | "timeRange" 
  ? [string, string] 
  : string;

export interface BaseFormItemConfigs {
  name: string;
  label: string;
  rules?: any[];
  type?: FormItemType;
  options?: any[];
  maxCount?: number;
  accept?: string;
  listType?: UploadProps['listType'];
  showTime?: boolean;
  max?: number;
  min?: number;
  mode?: any;
  help?: string;
  render?: (form: any, nameValue: any) => React.ReactNode;
  customNode?:
    | React.ReactNode
    | ((props: {
        value: any;
        onChange: (value: any) => void;
        form: any;
      }) => React.ReactNode);
  onClick?: (args: any) => void;
  row?: number;
  col?: number;
  initialValue?: any;
  dependencies?: string[];
  visible?: (values: any) => boolean;
  /** @deprecated 请使用initialValue   代替，defaultValue将在下一个大版本移除 */
  defaultValue?: any;
}

export interface SinglePlaceholderFormItemConfigs extends BaseFormItemConfigs {
  type?: Exclude<FormItemType, "dateRange" | "timeRange">;
  placeholder?: string;
}

export interface RangePlaceholderFormItemConfigs extends BaseFormItemConfigs {
  type: "dateRange" | "timeRange";
  placeholder?: [string, string];
}

export type FormItemConfigs = SinglePlaceholderFormItemConfigs | RangePlaceholderFormItemConfigs;

export type TSortOption = {
  field: string;
  order: "asc" | "desc";
};

export type EqualityFilter = {
  $eq?: string | number | boolean | null;
  $ne?: string | number | boolean;
  $gt?: number;
  $gte?: number;
  $lt?: number;
  $lte?: number;
  $in?: (string | number | boolean)[];
  $notIn?: (string | number | boolean)[];
  $contains?: string;
  $notContains?: string;
  $containsi?: string;
  $notContainsi?: string;
  $null?: boolean;
  $notNull?: boolean;
  $between?: [number, number];
  $startsWith?: string;
  $startsWithi?: string;
  $endsWith?: string;
  $endsWithi?: string;
  $or?: FilterCriteria[];
  $and?: FilterCriteria[];
  $not?: FilterCriteria;
};

export type FilterCriteria = {
  [key: string]: EqualityFilter;
};

export type TFilterConditions =
  | {
      $or?: FilterCriteria[];
      $and?: FilterCriteria[];
      $not?: FilterCriteria;
    }
  | FilterCriteria;

export type TPopulateOption = {
  [key: string]: {
    fields?: string[];
  };
};

export type TPageRequest = {
  sort?: string[];
  filters?: TFilterConditions;
  populate?: TPopulateOption;
  fields?: string[];
  pagination: {
    pageSize: number;
    page: number;
  };
  publicationState?: string;
  locale?: string[];
};

export interface ArticleData {
  _id: number;
  文章标题: string;
  文章分类: string;
  文章标签: string;
  文章作者: string;
  封面图: string;
  文章内容: string;
  是否删除: number;
  _create: string;
  _update: string;
}

export type MenuType = typeof MENU_TYPE[keyof typeof MENU_TYPE];

export interface MenuItem {
  id: number;
  key: string;
  name: string;
  parent_id: number;
  icon: string | React.ReactNode;
  type: MenuType;
  sort: number;
  page_path: string | null;
  link_path: string | null;
  api_path: string | null;
  is_disabled: number;
  is_deleted: number;
  created_at: string;
  updated_at: string;
}

export interface RolePermission {
  roleId: number;
  menuIds: number[];
  menuList: MenuItem[];
}

export interface UserData {
  id: number;
  username: string;
  email: string | null;
  phone: string | null;
  nickname: string | null;
  name: string | null;
  role_id: number;
  department_ids: number[];
  is_disabled: number;
  is_deleted: number;
  roleInfo: RolePermission;
}

export interface LoginData extends UserData {
  token: string;
}

export interface FieldIdentifiers {
  id: string;
  title: string;
  pId: string;
  key: string;
  path?: string;
}

export interface MenuItemType {
  danger?: boolean;
  disabled?: boolean;
  icon?: React.ReactNode;
  key: string;
  label?: React.ReactNode;
  title?: string;
  children?: MenuItemType[];
  path?: string;
}

export interface SubMenuType extends MenuItemType {
  children: MenuItemType[];
  popupClassName?: string;
  popupOffset?: [number, number];
  onTitleClick?: (params: {
    key: string;
    domEvent: React.MouseEvent<HTMLElement>;
  }) => void;
  theme?: "light" | "dark";
}

export interface SysConfigItem {
  value: string | number;
  label: string;
}
